package com.google.code.vannevar.client.ui.listeners;

import static com.google.code.vannevar.client.ui.util.Components.get;

import java.util.Date;

import com.google.gwt.user.client.Cookies;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.TextField;

public final class LogonListener extends ButtonListenerAdapter {

    @Override
    public void onClick(Button button, EventObject event) {
     
        // TODO logon
        saveURL();
        remember();
        ((Window) get("login-window")).close();
    }
    
    private void remember() {
        
        // TODO
    }

    private void saveURL() {
        
        String url = getURL();
        
        String name = "vannevar.login.url";
        String cookieValue = Cookies.getCookie(name);
        Date fourWeeks = new Date(new Date().getTime() + 2419200000L);
        
        if (cookieValue == null) {
            
            Cookies.setCookie(name, url, fourWeeks);
            
        } else {
            
            Cookies.setCookie(name, cookieValue + "," + url, fourWeeks);
        }
    }
    
    private String getURL() {
        
        return ((TextField) get("login-window.form.url")).getText().trim();
    }
}
